package com.mobilottery.simulation;

import java.util.Date;

import org.apache.log4j.Logger;

import com.mobilottery.server.GameProvider;
import com.mobilottery.ui.ValidationForm;

/**
 * Baking bean for game simulation form
 * @author fedor
 */
public class CreateGameForm extends ValidationForm
{
	float participationCost = 20;
	float maxPrize = 20000000;
    Date openDate = new Date();
    Date closeDate  = new Date(); 
    Date resultDate  = new Date();
    String description = "New lottery simulation";
    String createdId = null;
    //private static final long DAY_MILLISECONDS = 1000*60*60*24;
    
    public CreateGameForm()
    {
    	reset();
    }

	public String getParticipationCost() {
		return participationCost>0  ? PRICE_FORMAT.format(participationCost) : "";
	}

	public void setParticipationCost(String participationCost) 
	{
		this.participationCost = parseNumber("participationCost", participationCost, this.participationCost);
	}	


	public String getMaxPrize() {
		return maxPrize>0 ?  PRICE_FORMAT.format(maxPrize) : "";
	}

	public void setMaxPrize(String maxPrize) {
		this.maxPrize = parseNumber("maxPrize", maxPrize, this.maxPrize);
	}
    	
	
	public String getOpenDate() {
		return openDate!=null ? DATE_FORMAT.format(openDate) : "";
	}

	public void setOpenDate(String openDate) {
		this.openDate = parseDate("openDate", openDate, this.openDate);
	}

	
	public String getCloseDate() {
		return closeDate!=null ?  DATE_FORMAT.format(closeDate) : "";
	}

	public void setCloseDate(String closeDate) {
		this.closeDate = parseDate("closeDate", closeDate, this.closeDate);
	}

	public String getResultDate() {
		return resultDate!=null ? DATE_FORMAT.format(resultDate) : "";
	}

	public void setResultDate(String resultDate) {
		this.resultDate = parseDate("resultDate", resultDate, this.resultDate);
	}
		
	public String getDescription() {
		return description!=null ? description : "";
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public void validate()
	{
		checkNonEmpty("description", description);
		checkNonEmpty("participationCost", participationCost);
		checkNonEmpty("maxPrize", maxPrize);
		if(checkNonEmpty("openDate", openDate))
		{
			if(checkNonEmpty("closeDate", closeDate))
			{
				checkDatePast("closeDate", closeDate, openDate);
				checkDatePast("closeDate", closeDate, new Date());
				if(checkNonEmpty("resultDate", resultDate))
					checkDatePast("resultDate", resultDate, closeDate);
			}
		}
	}
	
	
	public void submit()
	{
		try
		{
			validate();
			if(!hasErrors())
			{
				LotterySystemSimulator simulator =  (LotterySystemSimulator)GameProvider.getInstance().getLotterySystem();
				String result = simulator.addSimulatedGame(description, participationCost, maxPrize, openDate, closeDate, resultDate);
				if(!result.startsWith("Error"))
				{
					createdId = result;
				}
				else
				{
					setFieldErorrMessage("form", result);					
				}
				
			}
		}
		catch(Exception ex)
		{
			Logger.getLogger(getClass()).error("Error processing register form", ex);
			setFieldErorrMessage("form", "Error processing request: " + ex.getMessage());
		}
	}
	
	public void touch()
	{
		super.reset();
    	createdId = null;
		description = null;
		participationCost = -1;
		maxPrize = -1;
	    openDate = null;
	    closeDate  = null; 
	    resultDate  = null;
	}
	
	public void reset()
	{
		super.reset();
    	createdId = null;
		description = "New game simulation";
		participationCost = 20;
		maxPrize = 20000000;
    	//set open date now
	    openDate = new Date();
	    closeDate  = new Date(); 
    	//set default close 2 minutes from now
    	closeDate.setTime(openDate.getTime() + 1000*60*2);
	    resultDate  = new Date();
    	//set default result as close
    	resultDate.setTime(closeDate.getTime());
	}

	public String getCreatedId() {
		return createdId;
	}

	public void setCreatedId(String createdId) {
		this.createdId = createdId;
	}



}
